package com.ecnu.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.utils.bean.BeanUtils;
import com.ecnu.system.domain.SysCommon;
import com.ecnu.system.domain.dto.BannerAddDTO;
import com.ecnu.system.domain.dto.BannerUpdateDTO;
import com.ecnu.system.domain.vo.ActivityVo;
import com.ecnu.system.domain.vo.BannerVo;
import com.ecnu.system.service.ISysCommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.ecnu.common.enums.CommonType.ACTIVITY_NAME;
import static com.ecnu.common.enums.CommonType.BANNER;

@Api(tags = "轮播图管理")
@RestController
@RequestMapping("/system/banner")
public class SysBannerController extends BaseController {

    @Autowired
    private ISysCommonService commonService;

    @ApiOperation(value = "新增轮播图")
    @PostMapping("/add")
    public AjaxResult addBanner(@RequestBody BannerAddDTO banner) {
        SysCommon sysBanner = new SysCommon();
        sysBanner.setType(BANNER.getCode());
        sysBanner.setCreateBy(getUsername());
        BeanUtils.copyProperties(banner, sysBanner);
        boolean save = commonService.save(sysBanner);
        if (!save) {
            return error("新增失败");
        }
        return success("新增成功");
    }

    @ApiOperation(value = "修改轮播图")
    @PutMapping("/update")
    public AjaxResult updateBanner(@RequestBody BannerUpdateDTO banner) {
        SysCommon sysBanner = new SysCommon();
        sysBanner.setUpdateBy(getUsername());
        BeanUtils.copyProperties(banner, sysBanner);
        sysBanner.setId(Long.parseLong(banner.getId()));
        boolean update = commonService.updateById(sysBanner);
        if (!update) {
            return error("修改失败");
        }
        return success("修改成功");
    }

    @ApiOperation(value = "查询轮播图列表（分页）")
    @GetMapping("/list")
    public AjaxResult listBanners(@RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "10") int size,
                                  @RequestParam String imgType) {
        Page<SysCommon> bannerPage = new Page<>(page, size);
        Page<SysCommon> paged = commonService.page(bannerPage, new QueryWrapper<SysCommon>()
                .eq("type", BANNER.getCode())
                .eq("img_type", imgType)
                .orderByDesc("sort"));
        // 将查询结果转换为BannerVo列表
        List<BannerVo> bannerVoList = paged.getRecords().stream().map(sysCommon -> {
            BannerVo bannerVo = new BannerVo();
            BeanUtils.copyProperties(sysCommon, bannerVo);
            bannerVo.setId(sysCommon.getId().toString());
            return bannerVo;
        }).collect(Collectors.toList());
        // 创建新的分页对象并返回
        Page<BannerVo> resultPage = new Page<>(paged.getCurrent(), paged.getSize(), paged.getTotal());
        resultPage.setRecords(bannerVoList);
        return success(resultPage);
    }

    @ApiOperation(value = "查询轮播图列表（不分页）")
    @GetMapping("/front")
    public AjaxResult listActivitiesFront(@RequestParam String imgType) {
        List<SysCommon> list = commonService.list(new LambdaQueryWrapper<SysCommon>()
                .eq(SysCommon::getStatus, 1)
                .eq(SysCommon::getType, BANNER.getCode())
                .eq(SysCommon::getImgType, imgType)
                .orderByDesc(SysCommon::getSort));
        // 将查询结果转换为ActivityVo列表
        List<ActivityVo> activityVoList = list.stream().map(sysCommon -> {
            ActivityVo activityVo = new ActivityVo();
            BeanUtils.copyProperties(sysCommon, activityVo);
            activityVo.setId(sysCommon.getId().toString());
            return activityVo;
        }).collect(Collectors.toList());
        return success(activityVoList);
    }


    @ApiOperation(value = "删除数据")
    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteBanner(@PathVariable String id) {
        boolean remove = commonService.removeById(Long.parseLong(id));
        if (!remove) {
            return error("删除失败");
        }
        return success("删除成功");
    }

    @ApiOperation(value = "根据ID查询轮播图详情")
    @GetMapping("/{id}")
    public AjaxResult getBannerById(@PathVariable String id) {
        SysCommon banner = commonService.getOne(new LambdaQueryWrapper<SysCommon>()
                .eq(SysCommon::getType, BANNER.getCode())
                .eq(SysCommon::getId, Long.parseLong(id)));
        if (banner == null) {
            return error("未找到对应的轮播图信息");
        }
        BannerVo bannerVo = new BannerVo();
        BeanUtils.copyProperties(banner, bannerVo);
        return success(bannerVo);
    }
}
